# To change this template, choose Tools | Templates
# and open the template in the editor.

class SyncBuildTask
  def initialize(build, job)
    @build = build
    @job = job
  end
  
  def execute
    tests_response = RestClient.post "http://lfjenkins.swacorp.com/jenkins/job/#{@job.name}/#{@build.number}/testReport/api/xml", :tree => "suites[cases[className,status]]"
    doc, test_results = REXML::Document.new(response), []
    
    doc.elements.each('testResult/suite/case') do |result|
      test_path = result.get_elements('className')[0].getText().value
      test_status = result.get_elements('status')[0].getText().value
      
      test = Test.find_by_path test_path
      if test.nil?
        test = Test.create(:path => test_path) #improve this transaction
      end
      
      test_results << TestResult.new do |t|
        t.result = test_status
        t.test = test        
      end
    end
    @build.test_results << test_results
  end
end
